﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MsForgeReboot.Framework.Entities;
using System.Net;
using System.IO;
using System.Net.Mail;

namespace MsForgeReboot.Framework.TownCrierChannelTools {
    public static class FormattingTools {

        public static string FormatEventToString(Event source) {
            throw new NotImplementedException();
        }

        public static RestSharp.Parameter[] FormatEventForTwitter(Event source) {
            StringBuilder buffer = new StringBuilder();
            buffer.AppendFormat("{0} on {1:dd/MM/yyyy} - {2}", source.OrganizedBy.Title, source.Begin.Value, source.Topic);

            //if (source.URL != null) {
            //    WebRequest req = WebRequest.Create(String.Format("http://tinyurl.com/api-create.php?url={0}", source.URL));
            //    HttpWebResponse response = req.GetResponse() as HttpWebResponse;
            //    string tinyUrl = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //    buffer.AppendFormat(" - {0}", tinyUrl);
            //}
            if (buffer.Length > 140)
                buffer.Remove(140, buffer.Length - 140);
            return new RestSharp.Parameter[] { new RestSharp.Parameter { Name = "status", Value = buffer.ToString(), Type = RestSharp.ParameterType.UrlSegment } };
        }

        /// <summary>
        /// Format event into a dictionary of string keys and values for Facebook data
        /// </summary>
        /// <param name="source">Event to be formatted</param>
        /// <returns>Dictionary of string, object</returns>
        public static Dictionary<string, object> FormatEventForFacebook(Event source) {
            if (source == null)
                throw new ArgumentException("Event must be a valid event", "Event");
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("name", source.Title);
            result.Add("start_time", source.Begin.Value.ToString("U"));
            result.Add("end_time", source.End.Value.ToString("U") ?? String.Empty);
            result.Add("location", source.OrganizedBy.Title ?? String.Empty);
            result.Add("street", source.Address.Street ?? String.Empty);
            result.Add("city", source.Address.City ?? String.Empty);

            return result;
        }



    }
}
